﻿using Hotel.Model;

namespace Hotel.DAL
{
    public class DisCountDAL
    {

        public DisCountDAL(MyConText myConText)
        {
            MyConText = myConText;
        }

        public MyConText MyConText { get; }



        /// <summary>
        /// 门店添加
        /// </summary>
        /// <param name="shopModel"></param>
        /// <returns></returns>
        public int ShopAdd(ShopModel shopModel)
        {
            
            try
            {
                MyConText.shops.Add(shopModel);
                return MyConText.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }



        }











        /// <summary>
        /// 门店显示
        /// </summary>
        /// <returns></returns>
        public List<ShopModel> ShopList()
        {

            try
            {
                return MyConText.shops.ToList();
            }
            catch (Exception)
            {

                throw;
            }
        }






        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="disCountModel"></param>
        /// <returns></returns>

        public int DisCountAdd(DisCountModel disCountModel)
        {
            try
            {
                MyConText.disCounts.Add(disCountModel);
                return MyConText.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }


        }


        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="count"></param>
        /// <param name="page"></param>
        /// <param name="Title">券标题模糊</param>
        /// <param name="ShopId">适用门店精确查询</param>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public List<DisCountModel> DisCountList(out int count, out int page, string? Title = "", int ShopId = -1, int index = 1, int size = 2)
        {

            try
            {
                var list = MyConText.disCounts.Where(x => x.IsDeletd == false).AsQueryable();
                if (!string.IsNullOrEmpty(Title))
                {
                    list = list.Where(x => x.Title.Contains(Title));

                }

                if (ShopId != -1)
                {

                    list = list.Where(x => x.ShopId == ShopId);

                }


                count = list.Count();
                page = (int)Math.Ceiling(count * 1.0 / size);
                return list.OrderBy(x => x.Id).Skip((index - 1) * size).Take(size).ToList();
            }
            catch (Exception)
            {

                throw;
            }

        }




        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public int DisCountDel(int Id)
        {

            try
            {
                var DisCountDels = MyConText.disCounts.Find(Id);
                DisCountDels.IsDeletd = true;
                return MyConText.SaveChanges();

            }
            catch (Exception)
            {

                throw;
            }


        }





        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public DisCountModel DisCountAll(int Id)
        {
            try
            {

                return MyConText.disCounts.Find(Id);
            }
            catch (Exception)
            {

                throw;
            }

        }




        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="disCountModel"></param>
        /// <returns></returns>
        public int DisCountUpd(DisCountModel disCountModel)
        {
            try
            {
                var DisCountUpds = MyConText.disCounts.Find(disCountModel.Id);
                if (DisCountUpds != null)
                {

                    DisCountUpds.DisCountType = disCountModel.DisCountType;
                    DisCountUpds.Title = disCountModel.Title;
                    DisCountUpds.Titles = disCountModel.Titles;
                    DisCountUpds.Photo = disCountModel.Photo;
                    DisCountUpds.Number = disCountModel.Number;
                    DisCountUpds.Count = disCountModel.Count;
                    DisCountUpds.Num = disCountModel.Num;
                    DisCountUpds.Money = disCountModel.Money;
                    DisCountUpds.CreateTime = disCountModel.CreateTime;
                    DisCountUpds.UpdateTime = disCountModel.UpdateTime;
                    DisCountUpds.YeWu = disCountModel.YeWu;
                    DisCountUpds.XuZhi = disCountModel.XuZhi;
                    DisCountUpds.ShopId = disCountModel.ShopId;
                    DisCountUpds.Phone = disCountModel.Phone;
                }
                return MyConText.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }


        }



    }
}